package com.chen.service.Impl;

import com.chen.exception.BusinessException;
import com.chen.mapper.ClassMapper;
import com.chen.mapper.EmpMapper;
import com.chen.mapper.StudentMapper;
import com.chen.pojo.ClassQueryParam;
import com.chen.pojo.ClassRoom;
import com.chen.pojo.Emp;
import com.chen.pojo.PageResult;
import com.chen.service.ClassService;
import com.chen.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<ClassRoom> page(ClassQueryParam classQueryParam) {
        PageHelper.startPage(classQueryParam.getPage(), classQueryParam.getPageSize());

        List<ClassRoom> rows = classMapper.list(classQueryParam);
        Page<ClassRoom> p = (Page<ClassRoom>) rows;
        return new PageResult<ClassRoom>(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(Integer id) {
        Integer count = studentMapper.countStudentByClassId(id);
        if(count>0){
            throw new BusinessException("该班级下有学生，不能删除");
        }
            classMapper.delete(id);
    }

    @Override
    public void add(ClassRoom classRoom) {
        classRoom.setCreateTime(LocalDateTime.now());
        classRoom.setUpdateTime(LocalDateTime.now());
//        classRoom.setMasterName(empMapper.getInfoById(classRoom.getMasterId()).getName());
        classMapper.add(classRoom);
    }

    @Override
    public ClassRoom getInfoById(Integer id) {
        return classMapper.getInfoById(id);
    }

    @Override
    public void updateInfo(ClassRoom classRoom) {
        classMapper.update(classRoom);
    }

    @Override
    public List<ClassRoom> findall() {
        return classMapper.findAll();

    }
}
